package com.cn.wanxi.servlet.back.company;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.DataField;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.service.impl.CompanyServiceImpl;
import com.cn.wanxi.util.Upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/company/intro")
public class CpyIntroServlet extends HttpServlet {
    ICompanyService companyService = new CompanyServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //公司信息
        resp.setContentType("application/json");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        CompanyModel company = companyService.getCompany();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("company",company);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //公司修改
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        ICompanyService companyService = new CompanyServiceImpl();
        CompanyModel company = new CompanyModel();
        company.setWork_time(req.getParameter("work_time"));
        company.setWebsite(req.getParameter("website"));
        company.setPhone(req.getParameter("phone"));
        company.setName(req.getParameter("name"));
        company.setIntro(req.getParameter("intro"));
        company.setFax(req.getParameter("fax"));
        company.setEnglish_name(req.getParameter("english_name"));
        company.setEmail(req.getParameter("email"));
        company.setCpy_intro( req.getParameter("cpy_intro"));
        company.setCopyright(req.getParameter("copyright"));
        company.setAddress(req.getParameter("address"));
        company.setAd(req.getParameter("ad"));
        company.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        company.setLogo(req.getParameter("logo"));
        System.out.println(company.getLogo());
        Upload.UploadOut(company.getLogo(), DataField.outPath,DataField.basePath);//获取解析后的参数列表
        int result = companyService.editCompany(company);
        resp.getWriter().print(result);
    }
}
